package com.yitao.cms.controller.product;

import com.skipper.base.bean.DataOutput;
import com.yitao.client.dto.product.ProductSkuDto;
import com.yitao.client.utils.NumberUtil;
import com.yitao.cms.config.aopLog.LogAnnotation;
import com.yitao.cms.dto.product.ProductSkuAddDto;
import com.yitao.cms.service.product.ProductCmsSkuService;
import com.yitao.cms.service.product.ProductCmsStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
  * @Author:Wwl
  * @Description:
  * @Date:16:59 2019/3/9
  */

@Api(value = "/cms/productCmsSku", description = "商品主功能运营后台相关接口", tags = {"cms-product-sku"})
@RestController
@RequestMapping(value = "/productCmsSku", produces = {"application/json"})
public class ProductCmsSkuController {
    @Autowired
    private ProductCmsSkuService productCmsSkuService;
    @Autowired
    private ProductCmsStockService productCmsStockService;
    @ApiOperation("产品添加第二步，保存价格及库存")
    @LogAnnotation(targetType = "productCmsSku-save", remark = "保存价格及库存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public DataOutput<ProductSkuAddDto> saveProductSKU(HttpServletRequest request, @RequestBody ProductSkuAddDto productSkuAddDto) {
        return productCmsSkuService.saveProductSKU(request, productSkuAddDto);
    }
    @ApiOperation("根据商品ID查询sku信息及库存信息")
    @RequestMapping(value = "/getProductSkuListByProductId", method = RequestMethod.POST)
    public DataOutput<ProductSkuDto> getProductSkuListByProductId(
            @ApiParam(value = "商品Id") @RequestParam(value = "productId") Long productId) {
        return new DataOutput(productCmsSkuService.getProductSkuListByProductId(productId));
    }
    @ApiOperation("修改SKU库存表单条数据")
    @LogAnnotation(targetType = "productCmsSku-updateStock", remark = "修改库存")
    @RequestMapping(value = "/updateProductSalesAndStockById", method = RequestMethod.POST)
    public DataOutput updateProductSalesAndStockById(
            HttpServletRequest request,
            @ApiParam(value = "skuId") @RequestParam(value = "skuId") Long skuId,
            @ApiParam(value = "价格") @RequestParam(value = "salesPriceYuan") Double salesPriceYuan,
            @ApiParam(value = "库存表") @RequestParam(value = "stock") Integer stock,
            @ApiParam(value = "商品外部条码") @RequestParam(value = "productOutsideBarcode",required = false) String productOutsideBarcode
    ) {
        return productCmsSkuService.updateProductSalesAndStockById(request, skuId, NumberUtil.yuanToCent(salesPriceYuan), stock,productOutsideBarcode);
    }

    @ApiOperation("批量修改库存及售价")
    @LogAnnotation(targetType = "productCmsSku-updateStockByProductId", remark = "批量修改库存")
    @RequestMapping(value = "/updateProductStockByProductId", method = RequestMethod.POST)
    public DataOutput updateProductStockByProductId(
            HttpServletRequest request,
            @ApiParam(value = "产品Id") @RequestParam(value = "productId") Long productId,
            @ApiParam(value = "价格") @RequestParam(value = "salesPriceYuan") Double salesPriceYuan,
            @ApiParam(value = "库存表") @RequestParam(value = "stock") Integer stock,
          @ApiParam(value = "商品外部条码") @RequestParam(value = "productOutsideBarcode",required = false) String productOutsideBarcode
    ) {
        return productCmsSkuService.updateProductSalesAndStockByProductId(request, productId, NumberUtil.yuanToCent(salesPriceYuan), stock, productOutsideBarcode);
    }
    @ApiOperation("删除sku信息")
    @LogAnnotation(targetType = "productCmsSku-delete", remark = "删除sku信息")
    @RequestMapping(value = "/deleteProductSKUById", method = RequestMethod.POST)
    public DataOutput deleteProductSKUById(HttpServletRequest request,
                                           @ApiParam(value = "skuId") @RequestParam(value = "skuId") Long skuId) {
        return productCmsSkuService.deleteProductSKUById(request, skuId);
    }
}
